package View.JFrame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

import org.jdesktop.swingx.JXPanel;

import Management.AdjusterMachine;
import View.MainUI;
import View.JTable.JAnimationTable;

public class JRenameFrame extends JFrame implements KeyListener{
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextField1;
    private AdjusterMachine am;
    private JAnimationTable jAnime;
    private File file;
    private int index;

    /** Creates new form NewJPanel */
    public JRenameFrame(int index, File file) {
        initComponents();
        this.file = file;
        this.index = index;
        jLabel4.setText(this.file.getName());
        getSuggestedName();
        jButton1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				am.reNameFile(jTextField1.getText());
				setVisible(false);
				jAnime.setDirectionary(jAnime.getDirectory());
			}
        });
        jButton2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
			}
        });
        jTextField1.addKeyListener(this);
    }

    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();

        jLabel1.setText(MainUI.s.oldName);

        jLabel2.setText(MainUI.s.newName);

        jButton1.setText(MainUI.s.ok);
        jButton1.setIcon(MainUI.s.okIcon);

        jButton2.setText(MainUI.s.cancel);
        jButton2.setIcon(MainUI.s.cancelIcon);
        
        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(106, 106, 106)
                        .addComponent(jButton1)
                        .addGap(26, 26, 26)
                        .addComponent(jButton2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(55, 55, 55)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
                            .addComponent(jLabel4))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap(36, Short.MAX_VALUE))
        );
        pack();
        setTitle(MainUI.s.renameFrameName);
        setIconImage(((ImageIcon) MainUI.s.renameFrameIcon).getImage());
      setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    }// </editor-fold>

    private String getSuggestedName() {
    	am = new AdjusterMachine(this.index, this.file);
    	jTextField1.setText(am.getAdjustedName());
    	return null;
    }

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
			am.reNameFile(jTextField1.getText());
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		
	}
	
	public void registJAnimationtable(JAnimationTable jAnime) {
		this.jAnime = jAnime;
	}
	
	/**
	 * used by JAnimatorPanel;
	 * if index != -1 , this.index = index;
	 * if f != -1, this.f = f;
	 * @param index
	 * @param f
	 */
	public void setFile(int index, File f) {
		if (index != -1) {
			this.index = index;
		}
		if (f != null) {
			this.file = f;
		}
		
		jLabel4.setText(this.file.getName());
        getSuggestedName();
	}
}
